<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="naogen" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用el-tree放置主体内容 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width:100%"
            >
              <el-col
                :span="
                  20"
              >
                <svg-icon icon-class="user" /> <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title=" isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <!-- 在弹层中放入子组件 -->
        <!-- v-if="showDialog"  关闭和打开弹层 销毁组件 -->
        <addOrEdit
          v-if="showDialog"
          :id="curId"
          ref="addOrEditRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="showDialog = false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils/index'

export default {
  components: {
    addOrEdit
  },
  props: {

  },
  data() {
    return {
      showDialog: false, // 控制弹层显示隐藏
      list: [], // 部门列表
      curId: '', // 保存当前部门id
      isEdit: '', // 新增子部门   false 添加部门   true 编辑部门
      originList: '' // 保存所有部门列表的一部分数据 (id,pid,name,code)
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 加载部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res.data.depts, 'res.data.depts')
        // 获取部门数据并取出 数组中每个对象的部门数据 (id,pid,name,code)数据
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 删除第一条数据 取出公司名称
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    hAdd(id) {
      this.showDialog = true
      this.isEdit = false
      this.curId = id
    },
    // 编辑部门
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
      // dom更新是异步的
      // this.$nextTick(() => {
      //   // 解决数据不更新的问题 this.$nextTick.addOrEditRef 可以获取子组件 直接操作子组件里面的一切
      //   console.log(this.$refs.addOrEditRef, 'addOrEditRef')
      //   this.$refs.addOrEditRef.loadDepartDetail()
      // })
    },
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    // 实现删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    }
  }
}
</script>

<style>
.el-dialog__header{
  background-color:#4d9efd;
  color:#fff
}
.el-dialog__title {
    color:#fff
}
</style>
